package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 员工管理controller
 * @author Xiao
 * @date 2021/9/21
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 后台管理-登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("用户登陆。。。");
        Employee emp = employeeService.login(employee);
        //③ 如果没有查询到则返回登录失败结果
        if (emp == null) {
            return R.error("用户名或密码错误！");
        }
        //④ 查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0) {
            return R.error("员工已禁用！");
        }
        //⑤ 登录成功，将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("loginEmp", emp.getId());
        return R.success(emp);
    }

    /**
     * 后台管理-退出登录
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        log.info("用户退出登录。。。");
        //清理Session中的用户id
        request.getSession().removeAttribute("loginEmp");
        //销毁session对象
        //request.getSession().invalidate();
        return R.success("登出成功！");
    }

    /**
     * 员工管理-添加员工
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> saveEmp(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("接收到要添加的员工：{}", employee);
        //添加用户
        employeeService.saveEmp(employee);
        return R.success("添加成功！");
    }

    /**
     * 员工管理-分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>> selectEmpByPageAndCondition(
            @RequestParam Integer page, Integer pageSize, String name) {
        log.info("page:{},pageSize:{},name:{}", page, pageSize, name);
        //调用service层的分页查询并返回结果
        Page<Employee> empPage = employeeService.selectEmpByPageAndCondition(page, pageSize, name);
        return R.success(empPage);
    }

    /**
     * 员工管理-根据id修改员工信息
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> changeStatus(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("要修改状态的用户：{}",employee);
        /*long id = Thread.currentThread().getId();
        log.info("线程id：{}", id);*/
        /*//获取当前登陆用户的信息
        Long loginEmpId = (Long) request.getSession().getAttribute("loginEmp");
        //设置修改者id和修改时间 --> 公共字段由自定义对象处理器和@TableFild注解实现自动填充
        employee.setUpdateUser(loginEmpId);
        employee.setUpdateTime(LocalDateTime.now());*/
        //调用service层方法修改状态
        employeeService.updateEmp(employee);
        return R.success("状态修改成功！");
    }

    /**
     * 员工管理-根据id查询员工对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getEmpById(@PathVariable String id) {
        log.info("当前要修改的用户id：{}",id);
        //调用service层根据id查询的方法获取employee对象并返回给页面回显
        Employee employee = employeeService.getEmpById(Long.parseLong(id));
        return R.success(employee);
    }
}
